package bid.yanhj.yanhaijiang.intent;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private Button jumpBtn;
    private EditText userNameEt, userPwdEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        jumpBtn = this.<Button>findViewById(R.id.jump_btn);
        userNameEt = this.<EditText>findViewById(R.id.user_name_et);
        userPwdEt = this.<EditText>findViewById(R.id.user_pwd_et);

        jumpBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到第二个页面
                Intent intent = new Intent();
                // 当前Activity跳转目标Activity
//                intent.setClass(MainActivity.this, SecondActivity.class);

                // 有Activity 就启动
                intent.setAction("bid.yanhj.yanhaijiang.intent.second");

                // 传值
                String userName = userNameEt.getText().toString();
                String userPwd = userPwdEt.getText().toString();

                intent.putExtra("userName", userName);
                intent.putExtra("userPwd", userPwd);

                // 启动intent
//                startActivity(intent);

                startActivityForResult(intent, 1);
            }
        });
    }


    /**
     * resultCode 返回什么样的状态
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        String user = data.getStringExtra("userName");
        String pwd = data.getStringExtra("pwd");

        userNameEt.setText(user);
        userPwdEt.setText(pwd);
    }
}
